<template>
  <div>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'"
               :close-on-click-modal="false"
               :visible.sync="visible">
      <el-form :model="dataForm"
               :rules="dataRule"
               ref="dataForm"
               @keyup.enter.native="dataFormSubmit()"
               label-width="80px">
        <el-form-item label="公司名称"
                      prop="name">
          <el-input v-model="dataForm.name"></el-input>
        </el-form-item>
        <el-form-item label="公司地址"
                      prop="name">
          <div>{{dataForm.address}}<span @click.stop="showMap" style="color: blue;margin-left: 20px">修改</span></div>
        </el-form-item>
        <el-form-item label="联系人"
                      prop="name">
          <el-input v-model="dataForm.contactName"></el-input>
        </el-form-item>
        <el-form-item label="联系电话"
                      prop="name">
          <el-input v-model="dataForm.contactMobile"></el-input>
        </el-form-item>
        <el-form-item label="公司简介"
                      prop="description">
          <tiny-mce v-model="dataForm.companyDesc"
                    ref="content"
                    style="width:600px"></tiny-mce>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
    <select-map-container ref="selectMapContainer" @selectLocation="selectLocation"></select-map-container>
  </div>
</template>

<script>
  import MulPicUpload from '@/components/mul-pic-upload'
  import TinyMce from '@/components/tiny-mce'
  import VideoUpload from '@/components/video-upload'
  import selectMapContainer from './selectMapContainer'
  export default {
    data () {
      return {
        visible: false,
        roleList: [],
        dataForm: {
          id: null,
          name: '',
          address: '',
          latitude:'',
          longitude:'',
          contactName: '',
          contactMobile: '',
          companyDesc: ''
        },
        dataRule: {
          name: [{
            required: true, message: '名称不能为空', trigger: 'blur'
          }]
        }
      }
    },
    components: {
      MulPicUpload,
      TinyMce,
      VideoUpload,
      selectMapContainer
    },
    methods: {
      init (row) {
        console.log(row)
        this.dataForm.id = row ? row.id : 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.dataForm.name = row.name
            this.dataForm.address = row.address
            this.dataForm.latitude = row.latitude
            this.dataForm.longitude = row.longitude
            this.dataForm.contactName = row.contactName
            this.dataForm.contactMobile = row.contactMobile
            this.dataForm.companyDesc = row.companyDesc
          }
        })
      },
      showMap(){
        let locationInfo = {
          formattedAddress:this.dataForm.address,
          point:{
            lng:this.dataForm.longitude,
            lat:this.dataForm.latitude
          }
        }
        this.$refs['selectMapContainer'].init(locationInfo)
      },
      selectLocation(e) {
        this.dataForm.latitude = e.latitude
        this.dataForm.longitude = e.longitude
        this.dataForm.address = e.formattedAddress
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(this.dataForm.id ? '/v1/factory/update' : '/v1/factory/add'),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({ data }) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        })
      }
    }
  }
</script>
